{template 'common/header'}
<div id="container"></div>

<script id="tpl_main" type="text/html">
		<div class="mui-content content-bg-w7">
		    <div class="money-icon">
		    	
		    </div>
			<input type="hidden" id="logid" value="<%logid%>" />
		    <div class="info-money">
		    	<div class="tittle-money">
		    		您的当前余额
		    	</div>
		    	<div class="money-detail">
		    		￥<%credit%>
		    	</div>
		    </div>
		    <input type="text" name="" id="money" value="" placeholder="请输入充值的金额" class="input-recharge"/>
			<%if wechat.success%><button type="button" class="mui-btn mui-btn-blue mui-btn-block btn-withdraw-wechat balance_sub1">微信支付</button><%/if%>
			<%if alipay.success%><button type="button" class="mui-btn mui-btn-blue mui-btn-block btn-withdraw-wechat balance_sub2">支付宝支付</button><%/if%>

		    <a class="mui-btn mui-btn-purple mui-btn-block btn-withdraw-record"  href="{php echo $this->createMobileUrl('member/log',array('type'=>0))}">充值记录</a>
		</div>
</script>
<style type="text/css">
	.pop-dialog {position: fixed;top: 0;left: 0;width: 100%;height: 100%;z-index: 999;display: none;}
	.pop-dialog .bg {width: 100%;height: 100%;position: absolute;top: 0;left: 0;opacity: 0.9;background-color: #000;}
	.pop-dialog .body {position: absolute;top: 0;right: 10%;width: 80%;padding-top: 46px;color: #fff;}
	.pop-dialog .body > p {line-height: 1.4;font-size: 14px;padding: 6px 0;}
	.pop-dialog .body img.icon {width: 36px;margin: 0 5px;display: inline-block;}
	.pop-dialog .body img.arrow {position: absolute;right: -10%;top: 0;display: block;margin-right: -13px;width: 70px;}
	i.icon {padding: 4px 12px;margin: 0 3px;border-radius: 3px;background-color: #3b3b3b;font-size: 14px;}
	a.btn {background-color: #3b3b3b;color: #fff;margin-top:15px;display: block;margin: 0 auto;padding: 10px 20px;border-radius: 3px;text-align: center;text-decoration: none;font-size: 16px;}
</style>

<div class="pop-dialog" onclick="$(this).hide();">
	<div class="bg"></div>
	<div class="body">
		<img class="arrow" src="../addons/ewei_shop/template/mobile/default/static/images/arrow.png">
		<p>1. 点击右上角 <i class="fa fa-ellipsis-h"></i> 或 <i class="fa fa-ellipsis-v"></i></p>
		<p>2. 由于在微信中无法打开支付宝付款页面，请选择在浏览器中打开，即可访问充值页面</p>
		<p>3. 充值成功后，请返回到这里点击下面的返回按钮</p>
		<a href="{php echo $this->createMobileUrl('member')}" class="btn">返回</a>&nbsp;

	</div>
</div>

<script language="javascript">
	function isWeiXin() {
		var ua = window.navigator.userAgent.toLowerCase();
		if (ua.match(/MicroMessenger/i) == 'micromessenger') {
			return true;
		} else {
			return false;
		}
	}

	require(['tpl', 'core'], function (tpl, core) {
		function rechargeok(type) {
			var logid = $('#logid').val();
			core.json('member/recharge', {
				op: 'complete',
				logid: logid,type:type
			}, function (pay_json) {
				if (pay_json.status == 1) {
					core.tip.show('充值成功!');
					location.href = core.getUrl('member');
					return;
				}
				core.tip.show(pay_json.result);
				$('.button').removeAttr('submitting');
			}, true, true);
		}

		core.json('member/recharge', {openid:"{$openid}"}, function (json) {
			var result = json.result;
			if (json.status != 1) {
				core.message(result, '', 'error');
				return;
			}
			$('#container').html(tpl('tpl_main', result));
			$('#logid').val(result.logid);
			if (result.alipay.success) {
				$('.balance_sub2').click(function () {
					if( isWeiXin() ) {
						$('.pop-dialog').show();
						return;
					}

					var money = $('#money').val();
					if (!$('#money').isNumber()) {
						core.tip.show('请输入数字金额!');
						return;
					}
					var logid = $('#logid').val();
					if (logid == '') {
						core.tip.show('请刷新重试!');
						return;
					}

					core.json('member/recharge', {op: 'recharge', openid:"{$openid}",type: 'alipay', money: money, logid: logid}, function (rjson) {
						if(rjson.status!=1){
							$('.button').removeAttr('submitting');
							core.tip.show(rjson.result);
							return;
						}
						location.href =  rjson.result.alipay.url;
					},true,true);

				})
			}
			if (result.wechat.success) {
				$('.balance_sub1').click(function () {
					if ($(this).attr('submitting') == '1') {
						return;
					}
					var money = $('#money').val();
					if (!$('#money').isNumber()) {
						core.tip.show('请输入数字金额!');
						return;
					}
					var logid = $('#logid').val();
					if (logid == '') {
						core.tip.show('请刷新重试!');
						return;
					}

					$('.button').attr('submitting', 1);
					core.json('member/recharge', {op: 'recharge', openid:"{$openid}",type: 'weixin', money: money, logid: logid}, function (rjson) {
						if(rjson.status!=1){
							$('.button').removeAttr('submitting');
							core.tip.show(rjson.result);
							return;
						}

						var wechat = rjson.result.wechat;
						require(['http://res.wx.qq.com/open/js/jweixin-1.0.0.js'], function (wx) {

							jssdkconfig = {php echo json_encode($_W['account']['jssdkconfig']); } || {jsApiList: []};
							jssdkconfig.debug = false;
							jssdkconfig.jsApiList = ['checkJsApi', 'chooseWXPay'];
							wx.config(jssdkconfig);
							wx.ready(function () {
								$('.button').removeAttr('submitting');
								var appid = wechat.appid?wechat.appid:wechat.appId;
								wx.chooseWXPay({
									'appId': appid,
									'timestamp': wechat.timeStamp,
									'nonceStr': wechat.nonceStr,
									'package': wechat.package,
									'signType': wechat.signType,
									'paySign': wechat.paySign,
									success: function (res) {
										rechargeok('wechat');
									}, fail: function (res) {
										alert(res.errMsg);
									}
								});
							});
						});
					}, true, true);

				});
			}
		}, true)
	});

</script>
{template 'common/footer'}